#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  04-fileControl-feature.sh
# Version	:  1.0
# Date		:  2020/06/01
# Author	:  Lz
# Email		:  lz843723683@gmail.com
# History	:     
#                  Version 1.0, 2020/06/01
# Function	:  测试三权分立 - 文件访问控制策略
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="三权分立(系统特性) - 文件访问控制策略"

HeadFile_Source_LTFLIB="${LIB_SSHAUTO}"

testuser1_fileControl="ltffileControl"
passwd1_fileControl="olleH717.12.#$"
userip_fileControl="localhost"
AddUserNames_LTFLIB="${testuser1_fileControl}"
AddUserPasswds_LTFLIB="${passwd1_fileControl}"


## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
        # 配置免密登录
        SshAuto_OneConfig_LTFLIB "${userip_fileControl}" "${testuser1_fileControl}" "${passwd1_fileControl}"
        TestRetParse_LTFLIB "配置免密登录" "True" "no" "yes"

        SshAuto_SetIpUser_LTFLIB "${userip_fileControl}" "${testuser1_fileControl}"
        TestRetParse_LTFLIB "设置默认IP和用户名" "True" "no" "yes"

	return ${TPASS}
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
	return ${TPASS}
}


## TODO : 测试用例
testcase_1(){
	local tmpfile_fileControl="${TmpTestDir_LTFLIB}/ltf_filecontrol_sopft"
	local tmpuser="$testuser1_fileControl"

	# 创建文件
	SshAuto_CmdLocalSec_LTFLIB "touch ${tmpfile_fileControl}" "no" "no"
        TestRetParse_LTFLIB "secadm 创建文件 ${tmpfile_fileControl} 成功" "False"

	# 普通用户写文件
	local wirtelog="Hello Kylin~"
	SshAuto_CmdDef_LTFLIB "echo \"${wirtelog}\" > ${tmpfile_fileControl}" "no" "yes"
        TestRetParse_LTFLIB "普通用户 $tmpuser 写文件 $tmpfile_fileControl" "False"

	# 使用secadm用户配置用户对目录去除权限
	local autority="rw"
	SshAuto_CmdLocalSec_LTFLIB "sudo setfacl -m u:${tmpuser}:${autority} ${tmpfile_fileControl}" "no" "no"
        TestRetParse_LTFLIB "设置普通用户 $tmpuser 对文件 ${tmpfile_fileControl} 权限为 ${autority}" "False"

	# 查看目录权限
	SshAuto_CmdLocalSec_LTFLIB "sudo getfacl $tmpfile_fileControl | grep \"user:${tmpuser}:${autority}\"" "no" "no"
        TestRetParse_LTFLIB "当前普通用户 $tmpuser 对文件 ${tmpfile_fileControl}权限为 ${autority}" "False"

	# 普通用户写文件
	SshAuto_CmdDef_LTFLIB "echo \"${wirtelog}\" > ${tmpfile_fileControl}" "no" "no"
	SshAuto_CmdDef_LTFLIB "cat ${tmpfile_fileControl} | grep \"${wirtelog}\"" "no" "no"
        TestRetParse_LTFLIB "普通用户 $tmpuser 可以写文件 $tmpfile_fileControl" "False"

	# 清除目录
	if [ -f "$tmpfile_fileControl" ];then
		SshAuto_CmdLocalSec_LTFLIB "rm -rf $tmpfile_fileControl" "yes" "no"
	fi
}

## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
	testcase_1

	return $TPASS
}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
